// var i;
// var $bundleConfig = require("./BundleConfig");
// var $resKeeper = require("./ResKeeper");
// var $commonUtils = require("./CommonUtils");
// var p = cc._decorator;
// var h = p.ccclass;
// var f = p.property;
// var g = p.inspector;
// var _ = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.touchNode = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onEnable = function () {
//         this.touchNode.width = cc.winSize.width;
//         this.touchNode.height = cc.winSize.height;
//         this.touchNode.active = !1;
//     };
//     t.prototype.onDisable = function () {
//         this.touchNode.active = !1;
//     };
//     t.prototype.hideTouchNode = function () {
//         this.node.active = !1;
//         this.touchNode.active = !1;
//     };
//     t.prototype.showAward = function (e, t, o, n) {
//         if (void 0 === n) {
//             n = 1;
//         }
//         this.node.x = e.x;
//         this.node.active = !0;
//         this.node.scale = 0;
//         cc.tween(this.node)
//             .to(
//                 0.25,
//                 {
//                     scale: 1
//                 },
//                 {
//                     easing: "backOut"
//                 }
//             )
//             .start();
//         this.touchNode.active = !0;
//         this.setAwardNode(t, o, n);
//     };
//     t.prototype.getNowJiangLi = function (e, t, o) {
//         var n = null;
//         if (1 == o) {
//             if ("1" == t) {
//                 n = e.awardbox1;
//             }
//             if ("2" == t) {
//                 n = e.awardbox2;
//             }
//             if ("3" == t) {
//                 n = e.awardbox3;
//             }
//         }
//         if (2 == o) {
//             if ("1" == t) {
//                 n = e.tiaozhanbox1;
//             }
//             if ("2" == t) {
//                 n = e.tiaozhanbox2;
//             }
//             if ("3" == t) {
//                 n = e.tiaozhanbox3;
//             }
//             if ("4" == t) {
//                 n = e.tiaozhanbox4;
//             }
//         }
//         return n;
//     };
//     t.prototype.setAwardNode = function (e, t, o) {
//         var n = this;
//         var i = this.getNowJiangLi(e, t, o);
//         var a = [
//             "ui/tu/powerIcon",
//             "ui/tu/goldIcon",
//             "ui/tu/diamondIcon",
//             "quality/suipian_1",
//             "quality/suipian_2",
//             "quality/suipian_3"
//         ];
//         var r = function (e, t) {
//             return __awaiter(n, void 0, void 0, function () {
//                 var o;
//                 var n;
//                 var i;
//                 var r;
//                 var c;
//                 var u;
//                 return __generator(this, function (s) {
//                     switch (s.label) {
//                         case 0:
//                             switch (
//                                 ((o = t.getChildByName("img").getComponent(cc.Sprite)),
//                                 (n = t.getChildByName("awardlb").getComponent(cc.Label)),
//                                 (i = ""),
//                                 (r = 0),
//                                 e[0])
//                             ) {
//                                 case 1:
//                                     i = "res/" + a[1];
//                                     r = e[1];
//                                     break;
//                                 case 2:
//                                     i = "res/" + a[2];
//                                     r = e[1];
//                                     break;
//                                 case 3:
//                                     i = "res/" + a[0];
//                                     r = e[1];
//                                     break;
//                                 case 5:
//                                     c = e[1];
//                                     r = e[2];
//                                     if (1 == c) {
//                                         i = "res/" + a[3];
//                                     }
//                                     if (2 == c) {
//                                         i = "res/" + a[4];
//                                     }
//                                     if (3 == c) {
//                                         i = "res/" + a[5];
//                                     }
//                             }
//                             return [4, this._loadRes($bundleConfig.BundleNames.Common, i, cc.SpriteFrame)];
//                         case 1:
//                             u = s.sent();
//                             o.spriteFrame = u;
//                             $commonUtils.default.LimitSpriteMaxSize(o, 60);
//                             n.string = "+" + r;
//                             return [2];
//                     }
//                 });
//             });
//         };
//         var u = this.node.children[0].children;
//         u.forEach(function (e) {
//             e.active = !1;
//         });
//         for (var p = 0; p < i.length; p++) {
//             var h = i[p];
//             var f = u[p];
//             if (!f) {
//                 f = cc.instantiate(u[0]);
//                 this.node.children[0].addChild(f);
//             }
//             f.active = !0;
//             r(h, f);
//         }
//         this.scheduleOnce(function () {
//             n.node.width = n.node.children[0].width;
//             var e = n.node.x;
//             var t = n.node.x - n.node.width / 2;
//             var o = n.node.x + n.node.width / 2;
//             if (t < -cc.winSize.width / 2) {
//                 n.node.x = -(cc.winSize.width / 2 - n.node.width / 2);
//             }
//             if (o > cc.winSize.width / 2) {
//                 n.node.x = cc.winSize.width / 2 - n.node.width / 2;
//             }
//             n.node.children[1].x = e - n.node.x;
//         });
//     };
//     __decorate([f(cc.Node)], t.prototype, "touchNode", void 0);
//     return __decorate([h], t);
// })($resKeeper.default);
// exports.default = _;


// import {BundleNames} from "./BundleConfig";
// import ResKeeper from "./ResKeeper";
// import CommonUtils from "./CommonUtils";

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名: AwardDisplay
//  */
// @ccclass
// class UI_BoxAward extends ResKeeper {
//     @property(cc.Node)
//     touchNode: cc.Node = null;

//     /**
//      * 当组件启用时调用，设置 touchNode 的宽高并隐藏
//      * 建议方法名: handleEnable
//      */
//     onEnable(): void {
//         this.touchNode.width = cc.winSize.width;
//         this.touchNode.height = cc.winSize.height;
//         this.touchNode.active = false;
//     }

//     /**
//      * 当组件禁用时调用，隐藏 touchNode
//      * 建议方法名: handleDisable
//      */
//     onDisable(): void {
//         this.touchNode.active = false;
//     }

//     /**
//      * 隐藏 touchNode 和当前节点
//      * 建议方法名: hideTouchNode
//      */
//     hideTouchNode(): void {
//         this.node.active = false;
//         this.touchNode.active = false;
//     }

//     /**
//      * 显示奖励节点并播放动画
//      * @param e 奖励节点位置
//      * @param t 奖励类型
//      * @param o 奖励数量
//      * @param n 奖励等级，默认为 1
//      * 建议方法名: displayAward
//      */
//     showAward(e: cc.Node, t: string, o: string, n: number = 1): void {
//         this.node.x = e.x;
//         this.node.active = true;
//         this.node.scale = 0;
//         cc.tween(this.node)
//             .to(0.25, { scale: 1 }, { easing: "backOut" })
//             .start();
//         this.touchNode.active = true;
//         this.setAwardNode(t, o, n);
//     }

//     /**
//      * 获取当前奖励节点
//      * @param e 奖励数据
//      * @param t 奖励类型
//      * @param o 奖励等级
//      * @returns 奖励节点
//      * 建议方法名: getCurrentAwardNode
//      */
//     getNowJiangLi(e: any, t: string, o: number): any {
//         let awardNode = null;
//         if (o === 1) {
//             if (t === "1") awardNode = e.awardbox1;
//             if (t === "2") awardNode = e.awardbox2;
//             if (t === "3") awardNode = e.awardbox3;
//         } else if (o === 2) {
//             if (t === "1") awardNode = e.tiaozhanbox1;
//             if (t === "2") awardNode = e.tiaozhanbox2;
//             if (t === "3") awardNode = e.tiaozhanbox3;
//             if (t === "4") awardNode = e.tiaozhanbox4;
//         }
//         return awardNode;
//     }

//     /**
//      * 设置奖励节点
//      * @param e 奖励数据
//      * @param t 奖励类型
//      * @param o 奖励等级
//      * 建议方法名: configureAwardNode
//      */
//     setAwardNode(e: any, t: string, o: number): void {
//         const awardNode = this.getNowJiangLi(e, t, o);
//         const iconPaths = [
//             "ui/tu/powerIcon",
//             "ui/tu/goldIcon",
//             "ui/tu/diamondIcon",
//             "quality/suipian_1",
//             "quality/suipian_2",
//             "quality/suipian_3"
//         ];

//         const updateAwardNode = async (awardData: any, node: cc.Node) => {
//             const sprite = node.getChildByName("img").getComponent(cc.Sprite);
//             const label = node.getChildByName("awardlb").getComponent(cc.Label);
//             let iconPath = "";
//             let amount = 0;

//             switch (awardData[0]) {
//                 case 1:
//                     iconPath = `res/${iconPaths[1]}`;
//                     amount = awardData[1];
//                     break;
//                 case 2:
//                     iconPath = `res/${iconPaths[2]}`;
//                     amount = awardData[1];
//                     break;
//                 case 3:
//                     iconPath = `res/${iconPaths[0]}`;
//                     amount = awardData[1];
//                     break;
//                 case 5:
//                     const quality = awardData[1];
//                     amount = awardData[2];
//                     if (quality === 1) iconPath = `res/${iconPaths[3]}`;
//                     if (quality === 2) iconPath = `res/${iconPaths[4]}`;
//                     if (quality === 3) iconPath = `res/${iconPaths[5]}`;
//                     break;
//             }

//             const spriteFrame = await this._loadRes(BundleNames.Common, iconPath, cc.SpriteFrame);
//             sprite.spriteFrame = spriteFrame;
//             CommonUtils.LimitSpriteMaxSize(sprite, 60);
//             label.string = `+${amount}`;
//         };

//         const children = this.node.children[0].children;
//         children.forEach(child => child.active = false);

//         for (let i = 0; i < awardNode.length; i++) {
//             let child = children[i];
//             if (!child) {
//                 child = cc.instantiate(children[0]);
//                 this.node.children[0].addChild(child);
//             }
//             child.active = true;
//             updateAwardNode(awardNode[i], child);
//         }

//         this.scheduleOnce(() => {
//             this.node.width = this.node.children[0].width;
//             const nodeX = this.node.x;
//             const leftBound = this.node.x - this.node.width / 2;
//             const rightBound = this.node.x + this.node.width / 2;

//             if (leftBound < -cc.winSize.width / 2) {
//                 this.node.x = -(cc.winSize.width / 2 - this.node.width / 2);
//             }
//             if (rightBound > cc.winSize.width / 2) {
//                 this.node.x = cc.winSize.width / 2 - this.node.width / 2;
//             }
//             this.node.children[1].x = nodeX - this.node.x;
//         });
//     }
// }

// export default UI_BoxAward;

import { BundleNames } from "./BundleConfig";
import ResKeeper from "./ResKeeper";
import CommonUtils from "./CommonUtils";

const { ccclass, property } = cc._decorator;

/**
 * 建议类名: AwardDisplay
 */
@ccclass
class ViewBoxAward extends ResKeeper {
    @property(cc.Node)
    touchNode: cc.Node = null;

    /**
     * 当组件启用时调用，设置 touchNode 的宽高并隐藏
     * 建议方法名: handleEnable
     */
    onEnable(): void {
        this.touchNode.width = cc.winSize.width;
        this.touchNode.height = cc.winSize.height;
        this.touchNode.active = false;
    }

    /**
     * 当组件禁用时调用，隐藏 touchNode
     * 建议方法名: handleDisable
     */
    onDisable(): void {
        this.touchNode.active = false;
    }

    /**
     * 隐藏 touchNode 和当前节点
     * 建议方法名: hideTouchNode
     */
    hideTouchNode(): void {
        this.node.active = false;
        this.touchNode.active = false;
    }

    /**
     * 显示奖励节点并播放动画
     * @param rewardNode 奖励节点位置
     * @param rewardType 奖励类型
     * @param rewardAmount 奖励数量
     * @param rewardLevel 奖励等级，默认为 1
     * 建议方法名: displayAward
     */
    displayAward(rewardNode: cc.Node, rewardType: string, rewardAmount: string, rewardLevel: number = 1): void {
        this.node.x = rewardNode.x;
        this.node.active = true;
        this.node.scale = 0;
        cc.tween(this.node)
            .to(0.25, { scale: 1 }, { easing: "backOut" })
            .start();
        this.touchNode.active = true;
        this.configureAwardNode(rewardType, rewardAmount, rewardLevel);
    }

    /**
     * 获取当前奖励节点
     * @param rewardData 奖励数据
     * @param rewardType 奖励类型
     * @param rewardLevel 奖励等级
     * @returns 奖励节点
     * 建议方法名: getCurrentAwardNode
     */
    getCurrentAwardNode(rewardData: any, rewardType: string, rewardLevel: number): any {
        let currentAwardNode = null;
        if (rewardLevel === 1) {
            if (rewardType === "1") currentAwardNode = rewardData.awardbox1;
            if (rewardType === "2") currentAwardNode = rewardData.awardbox2;
            if (rewardType === "3") currentAwardNode = rewardData.awardbox3;
        } else if (rewardLevel === 2) {
            if (rewardType === "1") currentAwardNode = rewardData.tiaozhanbox1;
            if (rewardType === "2") currentAwardNode = rewardData.tiaozhanbox2;
            if (rewardType === "3") currentAwardNode = rewardData.tiaozhanbox3;
            if (rewardType === "4") currentAwardNode = rewardData.tiaozhanbox4;
        }
        return currentAwardNode;
    }

    /**
     * 设置奖励节点
     * @param rewardData 奖励数据
     * @param rewardType 奖励类型
     * @param rewardLevel 奖励等级
     * 建议方法名: configureAwardNode
     */
    configureAwardNode(rewardData: any, rewardType: string, rewardLevel: number): void {
        const targetAwardNode = this.getCurrentAwardNode(rewardData, rewardType, rewardLevel);
        const iconPaths = [
            "ui/tu/powerIcon",
            "ui/tu/goldIcon",
            "ui/tu/diamondIcon",
            "quality/suipian_1",
            "quality/suipian_2",
            "quality/suipian_3"
        ];

        const updateRewardNode = async (rewardItem: any, node: cc.Node) => {
            const sprite = node.getChildByName("img").getComponent(cc.Sprite);
            const label = node.getChildByName("awardlb").getComponent(cc.Label);
            let iconPath = "";
            let amount = 0;

            switch (rewardItem[0]) {
                case 1:
                    iconPath = `res/${iconPaths[1]}`;
                    amount = rewardItem[1];
                    break;
                case 2:
                    iconPath = `res/${iconPaths[2]}`;
                    amount = rewardItem[1];
                    break;
                case 3:
                    iconPath = `res/${iconPaths[0]}`;
                    amount = rewardItem[1];
                    break;
                case 5:
                    const quality = rewardItem[1];
                    amount = rewardItem[2];
                    switch (quality) {
                        case 1:
                            iconPath = `res/${iconPaths[3]}`;
                            break;
                        case 2:
                            iconPath = `res/${iconPaths[4]}`;
                            break;
                        case 3:
                            iconPath = `res/${iconPaths[5]}`;
                            break;
                    }
                    break;
            }

            const spriteFrame = await this._loadRes(BundleNames.Common, iconPath, cc.SpriteFrame);
            sprite.spriteFrame = spriteFrame;
            CommonUtils.limitSpriteSize(sprite, 60);
            label.string = `+${amount}`;
        };

        const children = this.node.children[0].children;
        children.forEach(child => child.active = false);

        for (let i = 0; i < targetAwardNode.length; i++) {
            let child = children[i];
            if (!child) {
                child = cc.instantiate(children[0]);
                this.node.children[0].addChild(child);
            }
            child.active = true;
            updateRewardNode(targetAwardNode[i], child);
        }

        this.scheduleOnce(() => {
            this.node.width = this.node.children[0].width;
            const nodeX = this.node.x;
            const leftBound = this.node.x - this.node.width / 2;
            const rightBound = this.node.x + this.node.width / 2;

            if (leftBound < -cc.winSize.width / 2) {
                this.node.x = -(cc.winSize.width / 2 - this.node.width / 2);
            }
            if (rightBound > cc.winSize.width / 2) {
                this.node.x = cc.winSize.width / 2 - this.node.width / 2;
            }
            this.node.children[1].x = nodeX - this.node.x;
        });
    }
}

export default ViewBoxAward;